#!/usr/bin/perl -w

use strict;
use Socket;

my $domain = 'domain.com';
my $header = <<END;
authoritative;
ddns-update-style none;
allow booting;
allow bootp;
boot-unknown-clients false;

option domain-name "$domain";

END

my %parameter = (

  '192.168.1'   => { 'netmask' => '255.255.255.0',
                     'router'  => '192.168.1.1',
                     'ns'      => '192.168.1.1',
                   },

  '192.168.2'   => { 'netmask' => '255.255.255.0',
                     'router'  => '192.168.2.1',
                     'ns'      => '192.168.2.1',
                   },

);

my %mac;

sub by_ip {

  my ($a1, $a2, $a3, $a4) = split /\./, $a;
  my ($b1, $b2, $b3, $b4) = split /\./, $b;

  if ($a1 == $b1) {
    if ($a2 == $b2) {
      if ($a3 == $b3) {
        $a4 =~ s/^([0-9]+)/$1/;
        $b4 =~ s/^([0-9]+)/$1/;
        return $a4 <=> $b4;
      } else {
        return $a3 <=> $b3;
      }
    } else {
      return $a2 <=> $b2;
    }
  } else {
    return $a1 <=> $b1;
  }

}

my $empty = 1;

while (<>) {

  chomp;
  next if ((/^#/) || ($_ eq '') || (/^\s+$/));

  if (/^(([0-9a-f]{2}:){5}[0-9a-f]{2}) ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))/i) {

    $mac{$3} = $1;
    $empty = 0;

  } else {

    print "Invalid record in input file! ($_)\n";

  }

}

print $header;

if (! $empty) {
  print <<END;
shared-network internal {

END
}

my $network_new = 1;
my $network_changed = 1;
my $network_old = '';

foreach (sort by_ip keys(%mac)) {

  my ($network) = /^(.*)\.\d{1,3}$/; 

  next if ((! defined $parameter{$network}{netmask}) || (! defined $parameter{$network}{router}) || (! defined $parameter{$network}{ns}));

  if ($network_old ne '') {
    if ($network ne $network_old) {
      $network_changed = 1;
    } else {
      $network_changed = 0;
    }
  }

  my $hostname = gethostbyaddr(inet_aton($_),AF_INET);

  if (defined $hostname) {

    if ($hostname =~ s/([\w-]+)\..*//) {

      $hostname = $1;

      if ($network_changed && (! $network_new)) {
        print <<END;
                }

        }

END

      }

      if ($network_changed) {
        $empty = 0;
        print <<END;
        subnet $network.0 netmask $parameter{$network}{netmask} { 

                option broadcast-address $network.255;
                option routers $parameter{$network}{router};
                option domain-name-servers $parameter{$network}{ns};

                group {

END
      }

      print <<END;
                        host $hostname {
                                fixed-address $_;
                                hardware ethernet $mac{$_};
                        }

END

    } else {

      print STDERR "Invalid DNS record! ($hostname)\n";

    }

  } else {

    print STDERR "Can't resolve IP address! ($_)\n";

  }

  $network_old = $network;
  $network_new = 0;

}

if (! $empty) {

  print <<END;
                }

        }

}

END

}
